package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.crypto.digest.BCrypt;
import com.zzyl.constant.BedConstants;
import com.zzyl.dto.BedDto;
import com.zzyl.dto.LoginDto;
import com.zzyl.entity.Bed;
import com.zzyl.entity.User;
import com.zzyl.mapper.BedMapper;
import com.zzyl.mapper.LoginMapper;
import com.zzyl.service.BedService;
import com.zzyl.service.LoginService;
import com.zzyl.utils.JwtUtil;
import com.zzyl.vo.BedVo;
import com.zzyl.vo.LoginVo;
import com.zzyl.vo.UserVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class LoginServiceImpl implements LoginService {

    @Autowired
    private LoginMapper loginMapper;


    @Override
    public UserVo login(LoginDto loginDto) {
        UserVo vo = loginMapper.selectByUserName(loginDto.getUsername());
        if (vo == null) {
            return null;
        } else {
            if (BCrypt.checkpw(loginDto.getPassword(), vo.getPassword())) {
                Map<String, Object> tokenMap = new HashMap<>();
                tokenMap.put("username", vo.getUsername());
                vo.setUserToken(JwtUtil.createJWT("itheima", 600000, tokenMap));
                return vo;
            } else {
                return null;
            }
        }
    }
}

